<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" lay-filter="searchForm">
                    <div class="layui-form-item">
                        <div class="layui-inline" style="display: none">
                            <label class="layui-form-label">mainId</label>
                            <div class="layui-input-inline">
                                <input type="text" name="mainId" id="mainId" readonly autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="partnerId" autocomplete="off" placeholder="请输入具体ID"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">收货人姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="consigneeName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">手机号码</label>
                            <div class="layui-input-inline">
                                <input type="text" name="consigneePhoneNum" placeholder="请输入具体的编号" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">订单状态</label>
                            <div class="layui-input-inline">
                                <select id="orderStatus" name="orderStatus" lay-filter="orderStatus">
                                    <option value="">全部</option>
                                    <option value="待支付">待下单</option>
                                    <option value="待发货">待发货</option>
                                    <option value="已发货">已发货</option>
                                    <option value="已完成">已完成</option>
                                    <option value="已回收">已回收</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" id="deliverysBtn"
                        lay-event="deliverys" style="display: none"> 发货
                </button>
                <button class="layui-btn layui-btn-danger layui-btn-sm data-add-btn" id="changeLogisticsBtn"
                        lay-event="changeLogistics" style="display: none"> 改物流
                </button>
                <button class="layui-btn layui-btn-sm layui-btn-warm data-add-btn" id="sendNotifyBtn"
                        lay-event="goodsDeliveryNotify" style="display: none"> 发送短信
                </button>
                <button class="layui-btn layui-btn-warm layui-btn-sm data-add-btn" id="modifyReceivingInfoBtn"
                        lay-event="modifyReceivingInfo" style="display: none"> 修改收货信息
                </button>
                <button class="layui-btn layui-btn-sm" id="batchShipmentBtn" lay-event="batchShipment"
                        style="display: none">
                    批量发货
                </button>
                <button class="layui-btn layui-btn-sm data-add-btn" id="exportExcelBtn"
                        lay-event="exportExcel" style="display: none"> 导出
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../lib/SheetJS/xlsx.full.min.js" charset="utf-8"></script>
<script src="../../lib/common/common.js" charset="utf-8"></script>
<script src="../../lib/common/permissions.js" charset="utf-8"></script>
<script>
    let mainId = "";// 主文档id
    let subject = "";// 标题
    window.onload = function () {
        mainId = GetQueryString("mainId");
        subject = GetQueryString("subject");
        if (isNotNull(mainId)) {
            layui.use(['form', 'table'], function () {
                var $ = layui.jquery,
                    table = layui.table;
                $("#mainId").val(mainId);


                table.render({
                    elem: '#currentTableId',
                    url: '/provide/samples/sub/list',
                    method: 'GET',
                    where: {
                        mainId: mainId,
                        orderStatus: ''
                    },
                    contentType: 'application/x-www-form-urlencoded',
                    toolbar: '#toolbarDemo',
                    cols: [[
                        {type: "radio", fixed: 'left'},
                        {
                            field: 'numbering', title: '序号', width: 60, align: 'center', templet: function (d) {
                                return d.LAY_INDEX; //返回序号
                            }
                        },
                        {field: 'partnerId', title: '用户ID', align: 'center',},
                        {field: 'partnerName', title: '用户昵称', align: 'center'},
                        {field: 'createTime', title: '报名时间', sort: true, align: 'center'},
                        {
                            field: 'orderStatus', title: '订单状态', sort: true, align: 'center',
                            templet: function (d) {
                                if (d.orderStatus === '待支付') {
                                    return '<span style="color:#ff5722;">未下单</span>'
                                } else if (d.orderStatus === '待发货') {
                                    return '<span style="color:#ff0000;">待发货</span>'
                                } else if (d.orderStatus === '已发货') {
                                    return '<span style="color:#ffc40f;">已发货</span>'
                                } else if (d.orderStatus === '已完成') {
                                    return '<span style="color:#319b62;">已完成</span>'
                                } else if (d.orderStatus === '已回收') {
                                    return '<span style="color:#b1b1b1;">已回收</span>'
                                } else {
                                    return '';
                                }
                            }
                        },
                        {field: 'grade', title: '评价等级', sort: true, align: 'center'},
                        {field: 'reward', title: '评价报酬', sort: true, align: 'center'},
                        {field: 'consigneeName', title: '收货人姓名', align: 'center'},
                        {field: 'consigneePhoneNum', title: '手机号码', align: 'center'},
                        {field: 'consigneeAddress', title: '收货地址', align: 'center'},
                        {field: 'orderRemarks', title: '备注', align: 'center'},
                        {
                            field: 'LogisticsInfo',
                            title: '物流信息',
                            sort: true,
                            align: 'center',
                            templet: function (d) {
                                return '<span>' + d.courierName + ' ' + d.courierNumber + '</span>'
                            }
                        },
                    ]],
                    limits: [50, 100, 150, 200, 250, 300],
                    limit: 50,
                    page: true,
                    request: {
                        pageName: 'pageNum',
                        limitName: 'pageSize'
                    },
                    response: { //定义后端 json 格式，详细参见官方文档
                        statusName: 'code', //状态字段名称
                        statusCode: '200', //状态字段成功值
                        msgName: 'msg', //消息字段
                        countName: 'total', //总数字段
                        dataName: 'rows' //数据字段
                    },
                    done: function (res, curr, count) {
                        showBtn();
                    }
                });

            });
        } else {
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
        }
    }


    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.on('toolbar(currentTableFilter)', function (obj) {

            let checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
            var select_data = data[0];// 选中的
            if (obj.event !== 'exportExcel' && obj.event !== 'batchShipment') {
                if (select_data === void 0 || select_data === null) {
                    layer.msg("请至少选择一行数据");
                    return false;
                }
            }
            if (obj.event === 'deliverys') {  // 监听发货操作
                if (select_data.orderStatus !== '待发货') {
                    layer.msg('已发货，无需再处理！');
                    return false;
                }
                var index = layer.open({
                    title: '快递信息填写页面',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['40%', '50%'],
                    content: './provideSamples-changeLogistics.html?id=' + select_data.id,
                    end: function () {
                        //关闭弹框后触发
                        reloadTable()
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'changeLogistics') {
                let courierName = select_data.courierName;
                let courierNumber = select_data.courierNumber;
                if (select_data.orderStatus === '待支付') {
                    layer.msg('用户未支付，不可发货');
                    return false;
                }
                if (select_data.orderStatus === '待发货') {
                    layer.msg('订单还未发货，请先发货');
                    return false;
                }
                if (select_data.orderStatus === '已完成') {
                    layer.msg('订单已完成，不能进行更改发货操作');
                    return false;
                }
                if (select_data.orderStatus === '已回收') {
                    layer.msg('名额已回收，不可发货');
                    return false;
                }
                if (!isNotNull(courierName) || !isNotNull(courierNumber)) {
                    layer.msg('物流公司或物流单号为空，不能进行更改发货操作');
                    return false;
                }
                var index = layer.open({
                    title: '快递信息填写页面',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['40%', '50%'],
                    content: './provideSamples-changeLogistics.html?id=' + select_data.id,
                    end: function () {
                        //关闭弹框后触发
                        reloadTable()
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'modifyReceivingInfo') {
                if (select_data.orderStatus === '待支付') {
                    layer.msg('用户未支付，不可修改');
                    return false;
                }
                if (select_data.orderStatus === '已完成') {
                    layer.msg('订单已完成，不可修改');
                    return false;
                }
                if (select_data.orderStatus === '已回收') {
                    layer.msg('名额已回收，不可修改');
                    return false;
                }
                var index = layer.open({
                    title: '修改收货信息页面',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['80%', '80%'],
                    content: './modifyAddress.html?id=' + select_data.id,
                    end: function () {
                        //关闭弹框后触发
                        reloadTable()
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'exportExcel') { // 监听导出
                layer.confirm('确定导出购买记录吗？', function (index) {
                    let data = obj.config?.where
                    data.subject = subject;

                    let filename = "样品领取记录.xlsx";
                    if (isNotNull(subject)) {
                        filename = subject;
                        filename.replace("<", " ").replace(">", " ")
                            .replace("[", " ").replace("]", " ")
                            .replace("/", "-").replace("\\", "-")
                            .replace(":", "：").replace("*", " ")
                            .replace("?", "？").replace("|", " ")
                            .replace("\"", " ");

                        filename = filename + "-样品领取记录.xlsx";
                    }

                    downloadExcel('/provide/samples/sub/list/export', data, filename, 'POST')
                });
            } else if (obj.event === 'goodsDeliveryNotify') {
                let courierName = select_data.courierName;
                let courierNumber = select_data.courierNumber;
                if (select_data.orderStatus === '待支付') {
                    layer.msg('用户未支付，不可发送');
                    return false;
                }
                if (select_data.orderStatus === '待发货') {
                    layer.msg('订单还未发货，请先发货');
                    return false;
                }
                if (select_data.orderStatus === '已完成') {
                    layer.msg('订单已完成，不可发送');
                    return false;
                }
                if (select_data.orderStatus === '已回收') {
                    layer.msg('名额已回收，不可发货');
                    return false;
                }
                if (!isNotNull(courierName) || !isNotNull(courierNumber)) {
                    layer.msg('物流公司或物流单号为空，不可发送通知');
                    return false;
                }
                // 样品发货短信通知
                const requestData = {
                    sendTo: select_data.consigneeName,
                    phoneNumber: select_data.consigneePhoneNum,
                    commodity: select_data.goodsName,
                    company: select_data.courierName,
                    numbers: select_data.courierNumber
                }
                let index = layer.alert("确定向该用户发送发货通知？", {
                    title: '提示'
                }, function () {
                    let myMsg = layer.msg("正在处理...", {
                        icon: 16,
                        time: -1
                    })
                    $.ajax({
                        type: "post",
                        url: '/notify/send/goods/delivery',
                        data: JSON.stringify(requestData),
                        dataType: 'json',
                        contentType: 'application/json; charset=UTF-8',
                        success: function (req) {
                            if (req.code === '200') {
                                layer.msg('已发送');
                                layer.close(index);
                                // 关闭弹出层
                            } else {
                                layer.msg(req.msg);
                            }

                        },
                        error: function (e) {
                            layer.msg(e.msg);
                            layer.close(myMsg);//手动关闭
                        }
                    })

                });
            } else if (obj.event === 'batchShipment') {
                let formDate = form.val('searchForm');
                formDate.orderStatus = "待发货";
                let param = {
                    actions: 'SAMPLE_BATCH_DELIVERY',
                    queryVO: formDate,
                }
                const index = layer.open({
                    type: 2,
                    title: "批量发货",
                    content: '../common/import/importComponents.html',
                    area: ['274px', '140px'],
                    success: function (layero, index) {
                        // 获取子页面的iframe
                        var iframe = window['layui-layer-iframe' + index];
                        // 向子页面的全局函数child传参
                        let data = {
                            module: 'SAMPLE_BATCH_DELIVERY',
                            param: param,
                            templateName: subject + '样品批量发货模板.xlsx',
                            dataPreviewUrl: '../common/import/dataPreviewTable.html',
                            errTemplateName: subject + '样品发货失败数据.xlsx',
                        }
                        iframe.childParam(data);
                    },
                    end: function () {
                        //关闭弹框后触发
                        reloadTable()
                    }
                });
            }
        })

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            // 执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: data.field
            }, 'data');

            return false;
        });
    });

    /**
     * 重载表格数据
     */
    function reloadTable() {
        layui.use(['table'], function () {
            let table = layui.table;
            table.reload('currentTableId', {
                url: '/provide/samples/sub/list',
                done: function (res, curr, count) {
                    showBtn();
                }
            });
        })
    }

    function showBtn() {
        const btnRoles = {
            "deliverysBtn": 'admin,Platform Activities Specialist,Platform Activities Leader',
            "changeLogisticsBtn": 'admin,Platform Activities Specialist,Platform Activities Leader',
            "modifyReceivingInfoBtn": 'admin,Platform Activities Specialist,Platform Activities Leader',
            "exportExcelBtn": 'admin,Platform Activities Specialist,Platform Activities Leader',
            "sendNotifyBtn": 'admin,Platform Activities Specialist,Platform Activities Leader',
            "batchShipmentBtn": 'admin,Platform Activities Specialist,Platform Activities Leader'
        }
        buttonPermissions(btnRoles)
    }

</script>

</body>
</html>